$(function () {
    
    var cancelKeypress = false;
    if ( 'onhelp' in window ) {
        window.onhelp = function () {
            return false;
        };
    }
    
    $("input, select, textarea").keydown(function ( event ) {
        
        // tecla F1
        if ( event.keyCode === 112 ) {
            if ( window.event ) {
                // Coloca 0 para o IE
                window.event.keyCode = 0;
            }
            
            cancelKeypress = true;
            customHelp($(event.currentTarget));
            
            return false;
        }
    });
    
    // Firefox
    $("input, select, textarea").keypress(function () {
        if ( cancelKeypress ) {
            cancelKeypress = false;
            return false;
        }
    });
    
    function customHelp(element) {
        
        var titulo = element.data('helptitulo'),
        content = element.data('helpcontent'),
        keys = element.data('helpkeys');
        
        if(!titulo && !content) return false;
        
        if(keys !== undefined) {
            keys = keys.split(',');
            content += '<br><br><h4>Teclas de comandos disponíveis</h4>';
            $.each(keys, function(i, value) { 
                content += '<kbd>' + value + '</kbd>';
            });
        }
        
        element.popover({
            animation: false,
            trigger: 'manual',
            title: titulo,
            content: content
        }).popover('show');
            
        element.one('blur', function() {
            $(this).popover('hide');
        });
    }
});